using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using Microsoft.Practices.EnterpriseLibrary.Data;

namespace ParangonBiz.Data
{
	public partial class SiteContent : IComparable<SiteContent>, IEquatable<SiteContent>
	{
		public string Teaser { get; set; }
        public SiteContentType ContentType { get; set; }
		public List<FileAttach> FileAttachements { get; set; }

        protected static int MapDateFrom(DataRow dr)
        {
            return dr["CreateDateYear"] != DBNull.Value ? Convert.ToInt32(dr["CreateDateYear"]) : -1;
        }
        public static List<int> MapDateFrom(DataSet ds)
        {
            // Initialise Collection.
            List<int> objects = new List<int>();

            // Validation.
            if (ds == null)
                throw new ApplicationException("Cannot map to dataset null.");

            if (ds.Tables[TableName].Rows.Count == 0)
                return objects;

            if (ds.Tables[TableName] == null)
                throw new ApplicationException("Cannot find table [dbo].[SiteContent] in DataSet.");

            if (ds.Tables[TableName].Rows.Count < 1)
                throw new ApplicationException("Table [dbo].[SiteContent] is empty.");

            // Map DataSet to Instance.
            foreach (DataRow dr in ds.Tables[TableName].Rows)
            {
                objects.Add(MapDateFrom(dr));
            }

            // Return collection.
            return objects;
        }

        //public void SaveEmpty(int iContentTypeCode)
        //{
        //    Content = string.Empty;
        //    Title = string.Empty;
        //    ContentTypeCode = iContentTypeCode;
        //    Save();
        //}

        public void Save()
		{
			IsDeleted = false;

			Insert();
		}

		public void UpdateInstance()
		{
			LastModifiedDate = DateTime.Now;

			Update();
		}

		public void MarkAsDeleted()
		{
			IsDeleted = true;

			Update();
		}

		private void GetContentType()
		{
			if (ContentTypeCode.HasValue)
				ContentType = SiteContentType.GetSiteContentTypeByCode(ContentTypeCode.Value);
		}

		private void FillFileAttachList()
		{
			if (SiteContentId.HasValue)
				FileAttachements = new List<FileAttach>(FileAttach.ListBySiteContentId(SiteContentId.Value));
		}

        public static List<SiteContent> ListAllContent(int? contentTypeId)
		{
            return ListNumberedContent(contentTypeId, null, null);
		}

	    public static List<SiteContent> ListAllContent(int? contentTypeId, int? year)
	    {
            return ListNumberedContent(contentTypeId, year, null);
	    }

        public static List<SiteContent> ListNumberedContent(int? contentTypeId, int? count)
        {
            return ListNumberedContent(contentTypeId, null, count);
        }

        private static List<SiteContent> ListNumberedContent(int? contentType, int? year, int? count)
        {
            List<SiteContent> lSiteContent = Search(
                new SiteContent
                {
                    ContentTypeCode = contentType,
                    IsDeleted = false
                }, year, count);
            return lSiteContent;
        }

        public static List<int> ListAllContentYear(int? siteContentTypeId)
        {
            Database db = DatabaseFactory.CreateDatabase();
            const string sqlCommand = "[dbo].gspSiteContentYear_SEARCH";
            using (DbCommand dbCommand = db.GetStoredProcCommand(sqlCommand, siteContentTypeId))
            {
                using (DataSet ds = db.ExecuteDataSet(dbCommand))
                {
                    ds.Tables[0].TableName = TableName;
                    List<int> years = MapDateFrom(ds);
                    return years.Count > 0 ? years : new List<int> { DateTime.Now.Year };
                }
            }
        }

		public int CompareTo(SiteContent other)
		{
			return String.Compare(Title, other.Title, StringComparison.Ordinal);
		}

		public bool Equals(SiteContent other)
		{
			return SiteContentId == other.SiteContentId;
		}
	}
}